<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="118px">
      <el-form-item label="辅料编码" prop="flbm">
        <el-input
          v-model="queryParams.flbm"
          placeholder="请输入辅料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="辅料名称" prop="flmc">
        <el-input
          v-model="queryParams.flmc"
          placeholder="请输入辅料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="辅料类型" prop="fllx">
        <el-select v-model="queryParams.fllx" placeholder="请选择辅料类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_fllx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="领取日期">
        <el-date-picker
          v-model="daterangeLqsj"
          style="width: 209px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="领取人姓名" prop="lqrxm">
        <el-input
          v-model="queryParams.lqrxm"
          placeholder="请输入领取人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:flgl:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:flgl:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:flgl:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:flgl:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
    border
        	style="width: 100%"
        	:header-cell-style="{ fontSize: '13px', color: '#555',textAlign: 'center',background:'#E0E9F0' }"
        	:cell-style="{ textAlign: 'center' }"
    :data="flglList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="辅料编码" align="center" prop="flbm" width="150"/>
      <el-table-column label="辅料名称" align="center" prop="flmc" width="150"/>
      <el-table-column label="辅料类型" align="center" prop="fllx" width="120" />
      <el-table-column label="领取人姓名" align="center" prop="lqrxm" width="150"/>
      <el-table-column label="领取人工号" align="center" prop="lqrgh" width="100"/>
      <el-table-column label="数量" align="center" prop="sl" width="100"/>
      <el-table-column label="单价/元" align="center" prop="dj" width="100"/>
      <el-table-column label="总价/元" align="center" prop="zj" width="100"/>
      <el-table-column label="领取日期" align="center" prop="lqsj" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lqsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="125">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['flgl:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['flgl:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['flgl:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框1，添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="980px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
         <!--1，计划/订单信息 -->
        <el-form-item label="辅料编码" prop="flbm">
          <el-input v-model="form.flbm" placeholder="请输入辅料编码" />
        </el-form-item>
        <el-form-item label="辅料名称" prop="flmc">
          <el-input v-model="form.flmc" placeholder="请输入辅料名称" />
        </el-form-item>
        <el-form-item label="辅料类型" prop="fllx">
          <el-select v-model="form.fllx" placeholder="请选择物料类型">
            <el-option
              v-for="dict in dict.type.sys_fllx"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领取人姓名" prop="lqrxm">
          <el-input v-model="form.lqrxm" placeholder="请输入领取人姓名" />
        </el-form-item>
        <el-form-item label="领取人工号" prop="lqrgh">
          <el-input v-model="form.lqrgh" placeholder="请输入领取人工号" />
        </el-form-item>
        <el-form-item label="领取日期" prop="lqsj">
          <el-date-picker clearable
                          v-model="form.lqsj"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择下单日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="数量" prop="sl">
          <el-input v-model="form.sl" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="辅料单价/元" prop="dj">
          <el-input v-model="form.dj" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="辅料总价/元" prop="zj">
          <el-input v-model="form.zj" placeholder="请输入总价" />
        </el-form-item>
        <el-form-item label="领料原因" prop="llsm">
          <el-input v-model="form.llsm" placeholder="请输入领料原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listFlgl, getFlgl, delFlgl, addFlgl, updateFlgl } from "@/api/system/flgl";
import { getToken } from "@/utils/auth";
import { getCurrentDateTime } from "@/utils/dateutil";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Flgl",
  dicts: ['sys_ddzt', 'sys_shzt', 'sys_rkzt', 'sys_ckzt', 'sys_ddlx', 'sys_fllx', 'sys_yw_shifou'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      flmcs: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进销存_计划与订单管理表格数据
      flglList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 价格设置弹出层
      openJgsz: false,
      // 价格审批弹出层
      openJgsp: false,
      // 入库弹出层
      openRk: false,
      // 出库弹出层
      openCk: false,
      // 计划审核显示的表单元素
      isJgwhShow : true ,

      // 价格审核显示的表单元素
      isJgshShow : false ,
      // 发票ID时间范围
      daterangeLqsj: [],
      // 发票ID时间范围
      daterangeJhrq: [],

      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/flgl/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flbm: null,
        flmc: null,
        fllx: null,
        lqrxm: null,
        createby: null,
        createtime: null,
        updateby: null,
        updatetime: null,
        sfsc: "0"
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        flmc: [
          { required: true, message: "辅料名称不能为空", trigger: "blur" }
        ],
        fllx: [
          { required: true, message: "领取类型不能为空", trigger: "blur" }
        ],
        lqrxm: [
          { required: true, message: "领取人姓名不能为空", trigger: "blur" }
        ],
        lqsj: [
          { required: true, message: "领取日期不能为空", trigger: "blur" }
        ],
        sl: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        llsm: [
          { required: true, message: "领取原因不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询进销存_计划与订单管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeLqsj && '' != this.daterangeLqsj) {
        this.queryParams.params["beginLqsj"] = this.daterangeLqsj[0];
        this.queryParams.params["endLqsj"] = this.daterangeLqsj[1];
      }
      listFlgl(this.queryParams).then(response => {
        this.flglList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // 价格设置弹出层
      this.openJgsz = false;
      // 价格审批弹出层
      this.openJgsp = false;
      // 入库弹出层
      this.openRk = false;
      // 出库弹出层
      this.openCk = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        flid: null,
        flbm: null,
        flmc: null,
        fllx: null,
        lqrxm: null,
        lqrgh: null,
        lqsj: null,
        sl: null,
        dj: null,
        zj: null,
        createby: null,
        createtime: null,
        updateby: null,
        updatetime: null,
        sfsc: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeLqsj = [];
      this.daterangeJhrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.flid)
      this.flmcs = selection.map(item => item.flmc)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      // 价格设置弹出层
      this.openJgsz = false;
      // 价格审批弹出层
      this.openJgsp = false;
      // 入库弹出层
      this.openRk = false;
      // 出库弹出层
      this.openCk = false;
      this.title = "添加辅料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const flid = row.flid || this.ids
      getFlgl(flid).then(response => {
        this.form = response.data;
        this.open = true;
        // 价格设置弹出层
        this.openJgsz = false;
        // 价格审批弹出层
        this.openJgsp = false;
        // 入库弹出层
        this.openRk = false;
        // 出库弹出层
        this.openCk = false;
        this.title = "修改辅料管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.flid != null) {
            updateFlgl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
               this.openJgsz = false;
              this.getList();
            });
          } else {
            addFlgl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const flids = row.flid || this.ids;
      const flmcs = row.flmc || this.flmcs;
      this.$modal.confirm('是否确认删除辅料名称为"' + flmcs + '"的数据项？').then(function() {
        return delFlgl(flids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('flgl/export', {
        ...this.queryParams
      }, `订单管理_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('flgl/importTemplate', {
      }, `订单导入导入_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
@import "../../assets/styles/index.css";
::v-deep .el-popper .popper__arrow::after,
::v-deep .el-popper .popper__arrow {
	display: none;
}
::v-deep .el-upload-list {
	/* display: none; */
}
.el-icon-circle-close {
	font-size: 16px;
	color: #e65e5e;
	cursor: pointer;
	position: relative;
	top: 2px;
	left: 5px;
}
</style>
